package com.hut.oa.service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hut.oa.model.bean.Announce;
import com.hut.oa.model.bean.AnnounceUser;
import com.hut.oa.model.bean.Apply;
import com.hut.oa.model.bean.ApplyLeave;
import com.hut.oa.model.bean.ApplyObject;
import com.hut.oa.model.bean.ApplyObjectList;
import com.hut.oa.model.bean.ApplyRecord;
import com.hut.oa.model.bean.DeptOffice;
import com.hut.oa.model.bean.User;
import com.hut.oa.model.bean.WorkAttend;
import com.hut.oa.model.dao.apply.ApplyDao;
import com.hut.oa.model.dao.apply.ApplyLeaveDao;
import com.hut.oa.model.dao.apply.ApplyObjectDao;
import com.hut.oa.model.dao.apply.ApplyObjectListDao;
import com.hut.oa.model.dao.apply.ApplyRecordDao;
import com.hut.oa.model.dao.user.UserDao;
import com.hut.oa.model.dao.workattend.WorkAttendDao;

@Service
@Transactional(readOnly=true)
public class ApplyService {
	
	@Autowired
	ApplyLeaveDao ald;
	
	@Autowired
	ApplyObjectDao aod;
	
	@Autowired
	ApplyObjectListDao aold;
	
	@Autowired
	ApplyRecordDao ard;
	
	@Autowired
	ApplyDao ad;
	
	@Autowired
	UserDao ud;
	
	@Autowired
	WorkAttendDao wad;
	
	@Autowired
	AnnounceService as;
	
	/**
	 * 保存物品申请
	 * @param applyObject
	 */
	@Transactional
	public void saveObject(ApplyObject applyObject,User user){
		Long id=applyObject.getId();
		List<ApplyObjectList> aol=applyObject.getApplyObjectObject();
		for (ApplyObjectList applyObjectList : aol) {
			applyObjectList.setApplyObject(applyObject);
		}
		applyObject.setApplyObjectObject(aol);
		ApplyObject ao=aod.save(applyObject);//添加申请
		//添加一条记录
		if(id==null){
			ard.save(new ApplyRecord("提交申请",user,ao.getApply()));
		}else{
			ard.save(new ApplyRecord("修改申请",user,ao.getApply()));
		}
		//发送通知
		as.insertAnnounce(ao.getApply().getDeptOffice().getUsers(), ao.getApply().getStatus(), "你有一条新的申请动态", "/apply/show", ao.getApply().getId(), user);
	}
	
	/**
	 * 请假申请保存
	 * @param applyLeave
	 * @param user
	 * @throws UnknownHostException 
	 */
	@Transactional
	public void saveLeave(ApplyLeave applyLeave,User user){
		Long id=applyLeave.getId();
		//保存请假申请
		ApplyLeave myApplyLeave=ald.save(applyLeave);
		//添加一条记录
		if(id==null){
			ard.save(new ApplyRecord("提交申请",user,myApplyLeave.getApply()));
		}else{
			ard.save(new ApplyRecord("修改申请",user,myApplyLeave.getApply()));
		}
		//发送通知
		as.insertAnnounce(myApplyLeave.getApply().getDeptOffice().getUsers(), myApplyLeave.getApply().getStatus(), "你有一条新的申请动态", "/apply/show", myApplyLeave.getApply().getId(), user);
		
	}
	
	/**
	 * 获取两个日期之间的日期
	 * @param start
	 * @param end
	 * @return
	 */
	private List<Date> getBetweenDates(Date start, Date end) {
	    List<Date> result = new ArrayList<Date>();
	    Calendar tempStart = Calendar.getInstance();
	    tempStart.setTime(start);
	    
	    while(start.getTime()<=end.getTime()){
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
            start = tempStart.getTime();
        }
	    return result;
	}
	
	/**
	 * 申请审核
	 * @param approval
	 */
	@Transactional
	public void checkApply(DeptOffice office,int approval,Long applyId,String remark,User user) throws UnknownHostException{
		Apply apply=ad.findById(applyId);
		if(approval==0){//拒绝并退回
			apply.setStatus(2);//更改状态为已审核
			ard.save(new ApplyRecord("退回申请！"+remark, user, apply));//记录
		}else if(approval==1){//同意并转交
			apply.setStatus(1);//更改状态为审核中
			apply.setDeptOffice(office);//更改当前审批对象
			ard.save(new ApplyRecord("同意申请！并转交"+office.getName()+"！"+remark, user, apply));//记录
		}else{//同意并结束
			apply.setStatus(2);//更改状态为审核中
			ard.save(new ApplyRecord("同意申请！"+remark, user, apply));//记录
		}
		List<DeptOffice> offices=apply.getDeptOffices();
		offices.add(user.getDeptOffice());
		Apply myApply=ad.save(apply);
		
		//如果为请假申请，则获取请假时间，并添加数据到考勤内
		if(myApply.getApplyLeave()!=null){
			ApplyLeave myApplyLeave=myApply.getApplyLeave();
			List<Date> dates=this.getBetweenDates(myApplyLeave.getGoTime(), myApplyLeave.getBackTime());
			for (Date date : dates) {
				String[] ip=InetAddress.getLocalHost().toString().split("/");
				wad.save(new WorkAttend("请假", date,ip[1] , 5, myApply.getUser()));
			}
		}
		//发送通知
		List<User> users=myApply.getDeptOffice().getUsers();
		users.add(myApply.getUser());
		as.insertAnnounce(users, myApply.getStatus(), "你有一条新的申请动态", "/apply/show", myApply.getId(), user);
	}

}
